/**
 * HTTP请求相关类型定义
 * 统一维护所有HTTP请求的接口和类型
 */

import type { UseFetchOptions } from 'nuxt/app';
import type { FetchOptions } from 'ofetch';

/**
 * 请求配置接口
 */
export interface RequestConfig {
  // 是否显示加载状态
  showLoading?: boolean;
  // 是否显示错误提示
  showError?: boolean;
  // 请求超时时间（毫秒）
  timeout?: number;
  // 是否需要认证
  requireAuth?: boolean;
  // 重试次数
  retry?: number;
  // 重试延迟时间（毫秒）
  retryDelay?: number;
  // 是否启用缓存
  cache?: boolean;
  // 缓存时间（毫秒）
  cacheTTL?: number;
  // 自定义错误处理
  customErrorHandler?: (error: any) => void;
}

/**
 * 响应数据类型
 */
export interface ApiResponse<T = any> {
  code: number;
  data: T;
  message: string;
  success: boolean;
  timestamp?: number;
}

/**
 * HTTP错误类型
 */
export interface HttpErrorData {
  message: string;
  code: number;
  data?: any;
}

/**
 * 文件上传配置
 */
export interface UploadConfig extends RequestConfig {
  // 上传进度回调
  onProgress?: (progress: number) => void;
  // 允许的文件类型
  allowedTypes?: string[];
  // 最大文件大小（字节）
  maxSize?: number;
}

/**
 * 文件下载配置
 */
export interface DownloadConfig extends RequestConfig {
  // 下载进度回调
  onProgress?: (progress: number) => void;
  // 文件名
  filename?: string;
  // 是否自动下载
  autoDownload?: boolean;
}

/**
 * 请求拦截器配置
 */
export interface RequestInterceptor {
  onRequest?: (options: any, config: RequestConfig) => Promise<any> | any;
  onResponse?: <T>(
    response: ApiResponse<T>,
    config: RequestConfig
  ) => Promise<ApiResponse<T>> | ApiResponse<T>;
  onResponseError?: (error: any, config: RequestConfig) => Promise<void> | void;
}

/**
 * HTTP客户端配置
 */
export interface HttpClientConfig {
  baseURL: string;
  defaultConfig: RequestConfig;
  interceptors?: RequestInterceptor;
}

/**
 * 扩展的UseFetch选项
 */
export type ExtendedUseFetchOptions<T> = UseFetchOptions<ApiResponse<T>> &
  RequestConfig;

/**
 * 扩展的Fetch选项
 */
export type ExtendedFetchOptions = FetchOptions & RequestConfig;

/**
 * 请求方法类型
 */
export type HttpMethod =
  | 'GET'
  | 'POST'
  | 'PUT'
  | 'DELETE'
  | 'PATCH'
  | 'HEAD'
  | 'OPTIONS';

/**
 * 请求状态枚举
 */
export enum RequestStatus {
  IDLE = 'idle',
  PENDING = 'pending',
  SUCCESS = 'success',
  ERROR = 'error',
}

/**
 * HTTP状态码枚举
 */
export enum HttpStatusCode {
  OK = 200,
  CREATED = 201,
  NO_CONTENT = 204,
  BAD_REQUEST = 400,
  UNAUTHORIZED = 401,
  FORBIDDEN = 403,
  NOT_FOUND = 404,
  METHOD_NOT_ALLOWED = 405,
  CONFLICT = 409,
  UNPROCESSABLE_ENTITY = 422,
  TOO_MANY_REQUESTS = 429,
  INTERNAL_SERVER_ERROR = 500,
  BAD_GATEWAY = 502,
  SERVICE_UNAVAILABLE = 503,
  GATEWAY_TIMEOUT = 504,
}

/**
 * 缓存策略枚举
 */
export enum CacheStrategy {
  NO_CACHE = 'no-cache',
  CACHE_FIRST = 'cache-first',
  NETWORK_FIRST = 'network-first',
  CACHE_ONLY = 'cache-only',
  NETWORK_ONLY = 'network-only',
}

/**
 * 认证类型枚举
 */
export enum AuthType {
  BEARER = 'Bearer',
  BASIC = 'Basic',
  API_KEY = 'ApiKey',
}
